package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Cooperation;
import com.x.erp.rpc.CooperationRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 启辕合作信息Controller
 * 
 */
@RestController
@RequestMapping(value = "/cooperation")
public class CooperationController extends BaseController {

	@Reference
	private CooperationRpcService cooperationRpcService;// 启辕合作信息服务

	/**
	 * 新增启辕合作信息
	 * 
	 * @param cooperation
	 * @return
	 */
	@RequestMapping(value = "/insertCooperation", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel insertCooperation(Cooperation cooperation, HttpServletRequest request) {
		if (cooperation == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "启辕合作信息不能为空！");
		}
		if (StringUtils.isBlank(cooperation.getCooperationMobile())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "请填写您的手机号！");
		}
		List<Cooperation> cooperationList = cooperationRpcService.selectCooperationList(cooperation);
		if (cooperationList != null && cooperationList.size() > 0){
			return new ResultModel(ResultCode.RESULT_FAIL, "您已经留过您的联系方式！");
		}
		String source = request.getHeader("source");
		cooperation.setCooperationSource(source);
		cooperation.setCooperationState(0);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", cooperationRpcService.insertCooperation(cooperation));
	}

	/**
	 * 查询启辕合作信息列表
	 * @param cooperation
	 * @return
	 */
	@RequestMapping(value = "/selectCooperationList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectCooperationList(Cooperation cooperation) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", cooperationRpcService.selectCooperationList(cooperation));
	}

	/**
	 * 修改启辕合作信息
	 * @param cooperation
	 * @return
	 */
	@RequestMapping(value = "/updateCooperation", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel updateCooperation(Cooperation cooperation) {
		if (cooperation == null || StringUtils.isEmpty(cooperation.getCooperationId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "启辕合作ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功", cooperationRpcService.updateCooperation(cooperation));
	}

	/**
	 * 查询启辕合作信息数量
	 * @param cooperation
	 * @return
	 */
	@RequestMapping(value = "/selectCooperationNum", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectCooperationNum(Cooperation cooperation) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", cooperationRpcService.selectCooperationNum(cooperation));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}
}
